package com.ewili.x.special.service.data.impl;

import com.ewili.framework.utils.DateUtils;
import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.x.special.dao.inter.BaiduEventDao;
import com.ewili.x.special.dao.inter.BaiduEventStocksDao;
import com.ewili.x.special.entity.BaiduEvent;
import com.ewili.x.special.entity.BaiduEventStock;
import com.ewili.x.special.service.data.inter.AsyncService;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Service
public class AsyncServiceImpl implements AsyncService {

    @Autowired
    private BaiduEventDao baiduEventDao;

    @Autowired
    private BaiduEventStocksDao baiduEventStocksDao;

    //@Async
    @Override
    public void baiduEventUrlCode(String url, String dateStr) {
        try {
            Connection conn = Jsoup.connect(url).timeout(50000);
            Document document = conn.get();

            if (!document.outerHtml().contains("data-concept=")) {
                return;
            }
            Element masthead = document.select("div[data-concept]").first();
            String dataContent = masthead.attr("data-concept");
            String urlContent = "https://gupiao.baidu.com/concept/" + dataContent + ".html";
            this.getBaiduEvent(urlContent, dateStr);
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }
    }

    /**
     * 获取百度热点和对应股票
     *
     * @param urlContent
     */
    private void getBaiduEvent(String urlContent, String dateStr) {
        try {
            Connection conn = Jsoup.connect(urlContent).timeout(50000);
            Document document = conn.get();
            Element masthead = document.select("h1>a").first();
            String topic = masthead.text();

            List<Element> elements = document.select("div.comment>dl>dd");
            String event = elements.get(0).text();
            String chance = elements.get(1).text();

            BaiduEvent baiduEvent = new BaiduEvent();
            baiduEvent.setChance(chance);
            baiduEvent.setEvent(event);
            baiduEvent.setTopic(topic);
            baiduEvent.setDate(DateUtils.parseDate(dateStr, DateUtils.yyyy_MM_dd));
            baiduEventDao.saveAndFlush(baiduEvent);

            Element hotConcept = document.select("div.hot-concept").first();
            List<String> codes = hotConcept.select("ul>li>div>a>div.code").eachText();

            for (String code : codes) {
                BaiduEventStock baiduEventStock = new BaiduEventStock();
                baiduEventStock.setStockCode(code);
                baiduEventStock.setDate(DateUtils.parseDate(dateStr, DateUtils.yyyy_MM_dd));
                baiduEventStock.setEventId(baiduEvent.getId());
                baiduEventStocksDao.saveAndFlush(baiduEventStock);
            }
        } catch (IOException | ParseException e) {
            ExceptionUtils.propagate(e);
        }
    }
}
